package com.example.administrator.about_huanxin.presenter.activity;

import android.util.Log;

import com.example.administrator.about_huanxin.DataCacheUtil;
import com.example.administrator.about_huanxin.SimpelEmCallBack;
import com.example.administrator.about_huanxin.model.UserEntity;
import com.example.administrator.about_huanxin.view.activity.SearchActivityView;
import com.hyphenate.chat.EMClient;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2017/3/23 0023.
 */

public class SearchActivityPresenerImp implements SearchActivityPresener {

    private SearchActivityView searchActivityView;

    public SearchActivityPresenerImp(SearchActivityView searchActivityView) {
        this.searchActivityView = searchActivityView;
    }

    @Override
    public void searchFriend(String keywords) {
        BmobQuery<UserEntity> bmobQuery = new BmobQuery<>();
//        bmobQuery.addWhereContains("username",keywords);//注意:如果不是Bmob的收费用户,该方法失效
        bmobQuery
                .addWhereEqualTo("nickname", keywords)
//                .addWhereNotContainedIn("nickname", DataCacheUtil.mLists) //去除已经存在的好友(好友的列表集合)
                .findObjects(new FindListener<UserEntity>() {
            @Override
            public void done(List<UserEntity> list, BmobException e) {
                if (list != null && list.size()!=0 && e == null) {
                    Log.e("okokok", list.size() + "");
                    searchActivityView.getSearchResult(true,list);
                }else{
                    Log.e("nonono", e.toString());
                    searchActivityView.getSearchResult(false,null);
                }
            }
        });
    }

    @Override
    public void addFriendAction(final String username) {
        EMClient.getInstance().contactManager().aysncAddContact(username,"想认识下",new SimpelEmCallBack(){
            @Override
            public void onSuccess() {
                super.onSuccess();
                searchActivityView.getAddResult(true,0);
                Log.e("addFriend","add success"+username);
            }

            @Override
            public void onError(int i, String s) {
                super.onError(i, s);
                Log.e("addFriend",i+"...."+s);
                searchActivityView.getAddResult(true,0);
            }
        });
    }
}
